import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;

/**
 * Creater with IntelliJ IDEA.
 * Description:
 * User: 86152
 * Dae: 2022-07-18
 * Time: 18:27
 */
public class JDBC {
    public static void main(String[] args) throws SQLException {
        //1.使用DataSource描述MySql服务器位置
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("760121");

        //2.建立数据库连接
        Connection connection = dataSource.getConnection();

        //3.构造sql语句
        String sql = ("insert into student values(?,?)");
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //4.执行sql
        preparedStatement.setInt(1,6);
        preparedStatement.setString(2,"皇上");
        int n=preparedStatement.executeUpdate();
        System.out.println(n);
        //5.断开连接
        connection.close();
        preparedStatement.close();
    }
//    public static void main(String[] args) throws SQLException {
//        //1.使用DataSource描述MySql服务器位置
//        DataSource dataSource = new MysqlDataSource();
//        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
//        ((MysqlDataSource)dataSource).setUser("root");
//        ((MysqlDataSource)dataSource).setPassword("760121");
//
//        //2.建立数据库连接
//        Connection connection = dataSource.getConnection();
//
//        //3.构造sql语句
//        String sql = ("update book set price=61.20 where name<=>'深入理解Java虚拟机'");
//        PreparedStatement preparedStatement = connection.prepareStatement(sql);
//        //4.执行sql
//        int n=preparedStatement.executeUpdate();
//        System.out.println(n);
//        //5.断开连接
//        connection.close();
//        preparedStatement.close();
//    }
//    public static void main(String[] args) throws SQLException {
//        //1.使用DataSource描述MySql服务器位置
//        DataSource dataSource = new MysqlDataSource();
//        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
//        ((MysqlDataSource)dataSource).setUser("root");
//        ((MysqlDataSource)dataSource).setPassword("760121");
//
//        //2.建立数据库连接
//        Connection connection = dataSource.getConnection();
//
//        //3.构造sql语句
//        String sql = ("select borrow_info.id,book_id,student_id,start_time,end_time,book.name,author,price,category_id from borrow_info join book on category_id=3 and book.id=book_id");
//        PreparedStatement preparedStatement = connection.prepareStatement(sql);
//        //4.执行sql
//        ResultSet resultSet = preparedStatement.executeQuery();
//        while(resultSet.next()) {
//            int id = resultSet.getInt("id");
//            String book_id = resultSet.getString("book_id");
//            String student_id = resultSet.getString("student_id");
//            String start_time = resultSet.getString("start_time");
//            String end_time = resultSet.getString("end_time");
//            String name = resultSet.getString("name");
//            String author = resultSet.getString("author");
//            int price = resultSet.getInt("price");
//            int category_id =resultSet.getInt("category_id");
//            System.out.println(id+" "+book_id+" "+student_id+" "+start_time+" "+end_time+" "+name+" "+author+" "+price+" "+category_id);
//
//        }
//        //5.断开连接
//        connection.close();
//        preparedStatement.close();
//    }
//    public static void main(String[] args) throws SQLException {
//        //1.使用DataSource描述MySql服务器位置
//        DataSource dataSource = new MysqlDataSource();
//        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
//        ((MysqlDataSource)dataSource).setUser("root");
//        ((MysqlDataSource)dataSource).setPassword("760121");
//
//        //2.建立数据库连接
//        Connection connection = dataSource.getConnection();
//
//        //3.构造sql语句
//        String sql = "insert into borrow_info values(0,10,3,'2019-09-25 17:50','2019-10-25 17:50')";
//        PreparedStatement statement = connection.prepareStatement(sql);
//
//        //4.执行sql
//        int n = statement.executeUpdate();
//        System.out.println(n);
//
//        //5.断开连接
//        connection.close();
//        statement.close();
//    }

}
